//
//  ModuleManager.swift
//  SwfitDemo
//
//  Created by menchao on 2018/9/26.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

import UIKit

@objc public protocol ModuleManagerDelegate {
    
    //程序入口
    @objc optional func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey : Any]?)
    
    //其它的处理
    func applicationWillResignActive(_ application: UIApplication)
    func applicationDidEnterBackground(_ application: UIApplication)
}

public class ModuleManager : NSObject{
    static  let sharedInstace = ModuleManager.init()
    private override init(){
    }
    
    public func loadModule(_ module: ModuleManagerDelegate? ){
        if((module) != nil){
            self.allModules.append(module!)
        }
    }
    
    public func loadAllModule(_ modules:[Array<ModuleManagerDelegate>]?){
        if((modules) != nil){
            self.allModules.removeAll()
            for item in modules!{
                self.allModules.append(item as! ModuleManagerDelegate)
            }
        }
    }
    
    // MARK: - app delegate
    public  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey : Any]?){
        for  service in self.allModules {
            let serviceDelegate  =   service
            if serviceDelegate.application != nil{
                serviceDelegate.application!(application, didFinishLaunchingWithOptions: launchOptions)
            }
        }
    }
    
    lazy var allModules: Array<ModuleManagerDelegate> = {
        var array = Array<ModuleManagerDelegate>()
        return array
    }()
}




